/**
 *
 */
package com.imonsoft.common.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 定义了一些针对String的基本操作
 * 
 * @author LiQianHui
 * @date 2013-04-17
 * @version 2013
 */
public abstract class StringUtil extends StringUtils {
	// private final static Log logger = LogFactory.getLog(StringUtil.class);

	/**
	 * 对传入的字符串进行修正(null,>>>化为>>>null)
	 * 
	 * @param in
	 * @return
	 */
	public static String fixup(String in) {
		return in == null ? "" : in;
	}

	/**
	 * 对传入的对象进行修正(null||o.toString()为"null"==>>>null) AND ISO2GBK
	 * 
	 * @param o
	 * @return
	 */
	public static String fixup(Object o) {
		if (o instanceof java.sql.Timestamp) {
			String time = o.toString();
			return time.lastIndexOf("00:00:00.0") != -1 ? time.substring(0, 10)
					: time.substring(0, 19);

		}
		return o == null ? "" : o.toString();
	}

	/**
	 * 大于比较(greater than)
	 * 
	 * @param str1
	 * @param rule_value
	 * @return 如果str1>rule_value返回true
	 */
	public static boolean gt(String str1, String rule_value) {
		if (isBlank(str1) || isBlank(rule_value)) {
			return false;
		}
		try {
			double d1 = Double.valueOf(str1).doubleValue();
			double d2 = Double.valueOf(rule_value).doubleValue();
			return d1 > d2 && d2 != 0;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return false;
	}

	/**
	 * 大于比较(greater than)
	 * 
	 * @param str1
	 * @param rule_value
	 * @return 如果str1>rule_value返回true
	 */
	public static boolean gt(String str1, BigDecimal rule_value) {
		if (isBlank(str1)) {
			return false;
		}
		try {
			double d1 = Double.valueOf(str1).doubleValue();
			double d2 = rule_value.doubleValue();
			return d1 > d2 && d2 != 0;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return false;
	}

	/**
	 * 小于比较(less than)
	 * 
	 * @param str1
	 * @param rule_value
	 * @return 如果str1<rule_value返回true
	 */
	public static boolean lt(String str1, String rule_value) {
		if (isBlank(str1) || isBlank(rule_value)) {
			return false;
		}
		try {
			double d1 = Double.valueOf(str1).doubleValue();
			double d2 = Double.valueOf(rule_value).doubleValue();
			return d1 < d2 && d2 != 0;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return false;
	}

	/**
	 * 小于比较(less than)
	 * 
	 * @param str1
	 * @param rule_value
	 * @return 如果str1<rule_value返回true
	 */
	public static boolean lt(String str1, BigDecimal rule_value) {
		if (isBlank(str1)) {
			return false;
		}
		try {
			double d1 = Double.valueOf(str1).doubleValue();
			double d2 = rule_value.doubleValue();
			return d1 < d2 && d2 != 0;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return false;
	}

	/**
	 * 
	 * @param str
	 * @return
	 */
	public static double toDouble(String str) {
		if (isBlank(str)) {
			return 0;
		}
		try {
			double d = Double.valueOf(str).doubleValue();
			return d;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return 0;
	}

	public static Long toLong(String str) {
		if (isBlank(str)) {
			return null;
		}
		Long l = Long.valueOf(str);
		return l;
	}

	public static double toDouble(Object str) {
		if (str != null) {
			return toDouble(str.toString());
		}
		return 0;
	}

	public static Long[] splitToLongArray(String str) {
		if (str == null) {
			return null;
		}
		String[] strArr = str.split(",");
		Long[] longArr = new Long[strArr.length];
		for (int i = 0; i < strArr.length; i++) {
			longArr[i] = toLong(strArr[i]);
		}
		return longArr;
	}

	public static List<Long> splitToLongList(String str) {
		if (str == null) {
			return null;
		}
		List<Long> list = new ArrayList<Long>();
		String[] strArr = str.split(",");
		for (int i = 0; i < strArr.length; i++) {
			list.add(toLong(strArr[i]));
		}
		return list;
	}

	public static List<String> splitToStringList(String str) {
		return splitToStringList(str,",");
	}
	
	public static List<String> splitToStringList2(String str) {
		return splitToStringList(str,"&&");
	}

	/**
	 * 替换字符并返回字符List
	 * @param str 原字符串
	 * @param spStr 查找的字符串
	 * @return
	 */
	public static List<String> splitToStringList(String str,String spStr) {
		if (str == null||spStr==null) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		String[] strArr = str.split(spStr);
		for (int i = 0; i < strArr.length; i++) {
			list.add(strArr[i]);
		}
		return list;
	}
	public static Long[] stringArrayToLongArray(String[] strArr) {
		if (strArr == null) {
			return null;
		}
		Long[] longArr = new Long[strArr.length];
		for (int i = 0; i < strArr.length; i++) {
			longArr[i] = toLong(strArr[i]);
		}
		return longArr;
	}

	public static String iso2gbk(String str) {
		if (str == null) {
			return null;
		}
		try {
			return new String(str.getBytes("ISO-8859-1"), "GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String iso2utf(String str) {
		if (str == null) {
			return null;
		}
		try {
			return new String(str.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param str
	 * @return
	 */
	public static String oracleColumn2JavaField(String str) {
		if (str != null) {
			String[] strs = str.split("_");
			String toStr = strs[0].toLowerCase();
			for (int i = 1; i < strs.length; i++) {
				toStr += strs[i].substring(0, 1).toUpperCase()
						+ strs[i].substring(1).toLowerCase();
			}
			return toStr;
		}
		return null;
	}

	public static String javaField2OracleColumn(String str) {
		if (str != null) {
			String toStr = "";
			char[] chars = str.toCharArray();
			for (int i = 0; i < chars.length; i++) {
				if (Character.isUpperCase(chars[i])) {
					toStr += "_";
				}
				toStr += String.valueOf(chars[i]).toUpperCase();
			}
			return toStr;
		}
		return null;
	}

	public static boolean isNum(String str) {
		if (str == null) {
			return false;
		}
		char[] arr = str.trim().toCharArray();
		char c0 = '0';
		char c9 = '9';
		for (char c : arr) {
			if (c < c0 || c > c9) {
				return false;
			}
		}
		return true;
	}

	public static boolean isABC(String str) {
		if (str == null) {
			return false;
		}
		char[] arr = str.trim().toUpperCase().toCharArray();
		char c0 = 'A';
		char c9 = 'Z';
		for (char c : arr) {
			if (c < c0 || c > c9) {
				return false;
			}
		}
		return true;
	}

	/**
	 * base64 编码
	 * 
	 * @param s
	 * @return
	 */
	public static String base64encode(String s) {
		if (s == null) {
			return null;
		}
		BASE64Encoder encoder = new BASE64Encoder();
		s = encoder.encode(s.getBytes());
		s = s.replace(System.getProperty("line.separator"), "");
		return s;
	}

	/**
	 * base64解码
	 * 
	 * @param s
	 * @return
	 */
	public static String base64decode(String s) {
		if (s == null) {
			return null;
		}
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}

	public static String javaField2Getter(String field) {
		if (field != null) {
			return "get" + field.substring(0, 1).toUpperCase()
					+ field.substring(1) + "()";
		}
		return null;
	}

	public static String javaField2Setter(String field, String value) {
		if (field != null) {
			return "set" + field.substring(0, 1).toUpperCase()
					+ field.substring(1) + "(" + value + ")";
		}
		return null;
	}

	/*
	 * 将数字转为汉字，最大转换值为99999.99
	 */
	public static String num2china(String n) {
		if (isBlank(n)) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		StringBuffer buffer = new StringBuffer();
		if (n.length() != 0) {
			String[] c = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
			String[] d = { "万 ", "仟 ", "佰 ", "拾 ", "元 ", "角 ", "分 " };
			String[] ns = n.split("\\.");
			for (int i = 0; i < 5 - ns[0].length(); i++) {
				list.add(c[0]);
			}
			for (int i = 0; i < ns[0].length(); i++) {
				list.add(c[Integer.parseInt(ns[0].substring(i, i + 1))]);
			}
			if (ns.length == 2) {
				for (int i = 0; i < ns[1].length(); i++) {
					list.add(c[Integer.parseInt(ns[1].substring(i, i + 1))]);
				}
				for (int i = 0; i < 2 - ns[1].length(); i++) {
					list.add(c[0]);
				}
			}
			for (int i = 0; i < 5; i++) {
				buffer.append(list.get(i) + d[i]);
			}
			if (ns.length == 1) {
				for (int i = 5; i < 7; i++) {
					buffer.append(c[0] + d[i]);
				}
			}
			if (ns.length == 2) {
				for (int i = 5; i < 7; i++) {
					buffer.append(list.get(i) + d[i]);
				}
			}
		}
		return buffer.toString();
	}

    /**
     * 按照位数填充0
     * @param str
     * @param num
     * @return
     */
    public static String paddingZero(String str,int num){
        if(null==str){
            return "";
        }
        for (; str.length() < num;) {
            str = "0" + str;
        }
        return str;
    }

	public static void main(String[] args) {
		System.out.println(gt("12.1", "12"));
		System.out.println(lt("12.1", "12.2"));
		System.out.println(fixup(new BigDecimal(
				"323223232323232323237777732323.232323")));
		System.out.println(fixup(new Short("2323")));
		System.out.println(fixup(new Timestamp(System.currentTimeMillis())));

		System.out.println(base64encode("中文-犇"));
		System.out.println(base64decode("1tDOxC2gxA=="));

		System.out
				.println("OK:"
						+ base64decode("PFRjcEFjdDAwMj4KICA8dGNwQWN0Q29kZT4wMDI8L3RjcEFjdENvZGU+CiAgPGNtaXNObz45OTk5MDAwPC9jbWlzTm8+CiAgPGhpc0lucGF0aWVudFNuPjU1MDk0PC9oaXNJbnBhdGllbnRTbj4KICA8Y21pc0lucGF0aWVudFNuPjExODwvY21pc0lucGF0aWVudFNuPgogIDxib29rTm8+NDMwNzI0MTExMjAzMDAyODwvYm9va05vPgogIDxjYXJkTm8+PC9jYXJkTm8+CiAgPG1lbWJlclN5c25vPjQzMDcyNDExMTIwMzAwMjgwMDAxPC9tZW1iZXJTeXNubz4KICA8aW5wYXRpZW50Tm8+MDkwMDE2OTwvaW5wYXRpZW50Tm8+CiAgPGN1cmVJZD4yPC9jdXJlSWQ+CiAgPGNvdW50cnlUZWFtQ29kZT40MzA3MjQxMTEyMDM8L2NvdW50cnlUZWFtQ29kZT4KICA8ZmFtaWx5U3lzbm8+NDMwNzI0MTExMjAzMDAyODwvZmFtaWx5U3lzbm8+CiAgPG1lbWJlck5hbWU+1PjX5suzPC9tZW1iZXJOYW1lPgogIDxzZXhDb2RlPjE8L3NleENvZGU+CiAgPGFnZT48L2FnZT4KICA8aWRjYXJkTm8+PC9pZGNhcmRObz4KICA8ZGlzZWFzZUNvZGU+TTcxLjk5MTwvZGlzZWFzZUNvZGU+CiAgPGRpc2Vhc2VOYW1lPrusxNLR1yBOT1M8L2Rpc2Vhc2VOYW1lPgogIDxkaXNlYXNlQ29kZTI+PC9kaXNlYXNlQ29kZTI+CiAgPGRpc2Vhc2VOYW1lMj48L2Rpc2Vhc2VOYW1lMj4KICA8ZGlzZWFzZUNvZGUzPjwvZGlzZWFzZUNvZGUzPgogIDxkaXNlYXNlTmFtZTM+PC9kaXNlYXNlTmFtZTM+CiAgPGlub2ZmaWNlSWQ+MDI8L2lub2ZmaWNlSWQ+CiAgPG9mZmljZURhdGU+MjAwOS0wMS0wNzwvb2ZmaWNlRGF0ZT4KICA8aW5ob3NJZD4zPC9pbmhvc0lkPgogIDxjdXJlRG9jdG9yPjwvY3VyZURvY3Rvcj4KICA8YmVkTm8+PC9iZWRObz4KICA8c2VjdGlvbk5vPjwvc2VjdGlvbk5vPgogIDxvcHNJZD48L29wc0lkPgogIDx0dXJuT3Jnbm8+PC90dXJuT3Jnbm8+CiAgPHR1cm5PcmdOYW1lPjwvdHVybk9yZ05hbWU+CiAgPHR1cm5EYXRlPjwvdHVybkRhdGU+CiAgPHR1cm5PcD4wPC90dXJuT3A+CiAgPG1pbmlzdGVyTm90aWNlPjwvbWluaXN0ZXJOb3RpY2U+CiAgPHByb2NyZWF0ZU5vdGljZT48L3Byb2NyZWF0ZU5vdGljZT4KICA8Y2hyb25pY1Zpc2l0Tm8+PC9jaHJvbmljVmlzaXRObz4KICA8b3BlcmF0ZUVtcD54eXl5PC9vcGVyYXRlRW1wPgogIDxvcGVyYXRlVGltZT4yMDA5LTEyLTIxPC9vcGVyYXRlVGltZT4KPC9UY3BBY3QwMDI+"));

		System.out
				.println(base64encode("<WSResult><code>102</code><message>用户名或密码错误</message></WSResult>"));

		System.out.println(num2china("99999.99"));

		System.out.println(base64encode("2323"));
	}
}
